<template>
  <div class="search">
    <div class="search-box-wrapper">
      <search-box ref="searchBox" @query="onQueryChange"></search-box>
    </div>
    <div class="shortcutWrapper" ref="shortcutWrapper" v-show="!query">
      <scroll :data="shortcut" ref="shortcut" class="shortcut" :refreshDelay="refreshDelay">
        <div>
          <div class="hot-key">
            <h1 class="title">热门搜索</h1>
            <ul>
              <li @click="addQuery(item.k)" class="item" v-for="(item, index) in hotKey" :key="index">
                <span>{{item.k}}</span>
              </li>
            </ul>
          </div>
          <div class="search-history" v-show="searchHistory.length">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span @click="showConfirm" class="clear">
                <i class="icon-clear"></i>
              </span>
            </h1>
            <search-list @select="addQuery" @delete="deleteSearchHistory"></search-list>
          </div>
        </div>
      </scroll>
 
    </div>
    <div class="search-result" ref="searchResult" v-show="query">
      <suggest @select="saveSearch" @listScroll="blurInput" :query="query" ref="suggest"></suggest>
    </div>
    <confirm ref="confirm" @confirm="clearSearchHistory"></confirm>
    <router-view></router-view>
  </div>
</template>
<script>

import searchBox from 'base/search-box/search-box'
import { getHotKey } from 'api/search'
import { ERR_OK } from 'api/config'
import suggest from 'components/suggest/suggest'
import { mapActions } from 'vuex'
import searchList from 'base/search-list/search-list'
import confirm from 'base/confirm/confirm'
import scroll from 'base/scroll/scroll'
import { playlistMixin, searchMixin } from 'common/js/mixin'

export default {
  mixins: [playlistMixin, searchMixin],
  data() {
    return {
      hotKey: []
    }
  },
  components: {
    searchBox,
    suggest,
    searchList,
    confirm,
    scroll
  },
  created() {
    this._getHotKey()
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''

      this.$refs.searchResult.style.bottom = bottom
      this.$refs.suggest.refresh()

      this.$refs.shortcutWrapper.style.bottom = bottom
      this.$refs.shortcut.refresh()
    },
    showConfirm() {
      this.$refs.confirm.show()
    },
    _getHotKey() {
      getHotKey().then((res) => {
        if (res.code === ERR_OK) {
          this.hotKey = res.data.hotkey.slice(0, 10)
        }
      })
    },
    ...mapActions([
      'clearSearchHistory'
    ])
  },
  watch: {
    // 当query为空的时候应该手动刷新scroll组件
    query(newQuery) {
      if (!newQuery) {
        setTimeout(() => {
          // 等dom 加载完毕再刷新
          this.$refs.shortcut.refresh()
        }, 20)
      }
    }
  },
  computed: {
    // 传给scroll组件的值，当它改变的时候，要刷新scroll组件
    shortcut() {
      return this.hotKey.concat(this.searchHistory)
    }
  }
}
</script>
<style lang="stylus" scoped>
.search
  .search-box-wrapper
    margin: 20px
  .shortcutWrapper
    position: fixed
    top: 178px
    bottom: 0
    width: 100%
    .shortcut
      height: 100%
      overflow: hidden
      .hot-key
        margin: 0 20px 20px
        .title
          font-size: 14px
          color: hsla(0, 0%, 100%, 0.5)
          margin-bottom: 20px
        .item
          display: inline-block
          font-size: 14px
          padding: 5px 10px
          margin: 0 20px 10px 0
          color: hsla(0, 0%, 100%, 0.3)
          background: #333
          border-radius: 6px
      .search-history
        position: relative
        margin: 0 20px
        .title
          font-size: 14px
          line-height: 40px
          color: hsla(0, 0%, 100%, 0.5)
          display: flex
          align-items: center
          .text
            flex: 1
          .clear
            font-size: 14px
            color: hsla(0, 0%, 100%, 0.3)
  .search-result
    position: fixed
    top: 178px
    bottom: 0
    width: 100%
</style>